package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.AdminLicenseEntity;
import tech.abing.spider.task.utils.TaskUtil;
import tech.abing.spider.util.UuIdPublic;

public class AdminLicenseProcessor implements PageProcessor{
	
	private static Logger logger = Logger.getLogger(AdminLicenseProcessor.class);
	
	private static int record = 0;

	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		PageRequest request = page.getRequest();
		if(isFirstUrl(request.getUrl())){
			// 如果是首页，构造翻页的url压入队列
			// 最多100页，之后的都请求不到
			for(int i = 1; i < 100; i++){
				String url = getNextUrl(request.getUrl(), i);
				PageRequest req = context.createPageRequest(url, AdminLicenseProcessor.class);
				req.setHeaders(request.getHeaders());
				queue.add(req);
			}
			
		}
		String type = "行政许可";
		if (request.getUrl().contains("3284/3566")) {
			type = "行政许可";
		} else if (request.getUrl().contains("3284/3565")) {
			type = "通知公告";
		} 
		
		Elements table = doc.select("div.row");
		if(table !=null && table.size()>0){
			for (Element element : table) {
				AdminLicenseEntity license = new AdminLicenseEntity();
				license.setId(UuIdPublic.timeStrongID(5));
				license.setCreateTime(new Timestamp(System.currentTimeMillis()));
				license.setTitle(element.select("li.mc").text());
				String linkUrl = element.select("li.mc>div>a").attr("href");
				
				license.setLinkUrl(getUrl(request.getUrl(), linkUrl));
				license.setPostDate(element.select("li.fbrq").text());
				license.setDocumentNumber(element.select("li.wh").text());
				license.setUrl(request.getUrl());
				license.setType(type);
				objectContainer.add(license);
				
				String linkUrls=getUrl(request.getUrl(), linkUrl);
				PageRequest req = context.createPageRequest(linkUrls, AdminLicenseDetailProcessor.class);
				queue.add(req);
			}
		}
	}
	
	private String getUrl(String url, String linkUrl) 
	{
		int t = url.indexOf("zjhpublicof");
		String ts = url.substring(t, url.length());
		String tss = ts.substring(0, ts.indexOf("/"));
		String tmp = url.substring(0,t) + tss + "/";
		String tmp1 = linkUrl.substring(linkUrl.lastIndexOf("../")+3, linkUrl.length());
		return tmp + tmp1;
	}
	
	private String getNextUrl(String url, int index) 
	{
		String s = url.substring(0, url.length()-4);
		return s + "_" +  index + ".htm";
	}


	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	/**
	 * 判断请求的url是否是首页
	 * @param url
	 * @return
	 */
	private boolean isFirstUrl(String url) 
	{
		int index = url.indexOf("index");
		String tmp = url.substring(index, url.length());
		char[] chars = tmp.toCharArray();
		int num = 0;
		for (char c : chars) {
			if (String.valueOf(c).equals("_")) {
				num++;
			}
			if (num == 2) {
				break;
			}
		}
		return num != 2;
	}
	
}
